<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>vuex学习 | 诶无痕和诶有痕</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="vuex介绍● state，驱动应用的数据源；相当于组件中的data● view，以声明方式将 state 映射到视图；相当于组件中的template● actions，响应在 view 上的用户输入导致的状态变化。相当于组件中的methods 安装1234vue add vuex通过这种方式安装会自动配置相关信息npm install vuex --save通过这种方式安装，要自己配置vuex的">
<meta property="og:type" content="article">
<meta property="og:title" content="vuex学习">
<meta property="og:url" content="http://example.com/2022/03/23/vuex%E5%AD%A6%E4%B9%A0/index.html">
<meta property="og:site_name" content="诶无痕和诶有痕">
<meta property="og:description" content="vuex介绍● state，驱动应用的数据源；相当于组件中的data● view，以声明方式将 state 映射到视图；相当于组件中的template● actions，响应在 view 上的用户输入导致的状态变化。相当于组件中的methods 安装1234vue add vuex通过这种方式安装会自动配置相关信息npm install vuex --save通过这种方式安装，要自己配置vuex的">
<meta property="og:locale" content="zh_CH">
<meta property="og:image" content="http://example.com/images/19.png">
<meta property="article:published_time" content="2022-03-23T08:48:00.000Z">
<meta property="article:modified_time" content="2022-03-23T14:07:22.402Z">
<meta property="article:author" content="诶无痕和诶有痕">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://example.com/images/19.png">
  
    <link rel="alternate" href="/atom.xml" title="诶无痕和诶有痕" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/css/style.css">

<meta name="generator" content="Hexo 5.4.1"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">诶无痕和诶有痕</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://example.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-vuex学习" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2022/03/23/vuex%E5%AD%A6%E4%B9%A0/" class="article-date">
  <time datetime="2022-03-23T08:48:00.000Z" itemprop="datePublished">2022-03-23</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      vuex学习
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h1 id="vuex介绍"><a href="#vuex介绍" class="headerlink" title="vuex介绍"></a>vuex介绍</h1><p>● state，驱动应用的数据源；相当于组件中的data<br>● view，以声明方式将 state 映射到视图；相当于组件中的template<br>● actions，响应在 view 上的用户输入导致的状态变化。相当于组件中的methods<br><img src="/images/19.png" alt="img"></p>
<h1 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h1><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">vue add vuex</span><br><span class="line">通过这种方式安装会自动配置相关信息</span><br><span class="line">npm install vuex --save</span><br><span class="line">通过这种方式安装，要自己配置vuex的相关信息</span><br></pre></td></tr></table></figure>
<h1 id="使用"><a href="#使用" class="headerlink" title="使用"></a>使用</h1><ol>
<li>Vuex 的状态存储是响应式的。当 Vue 组件从 store 中读取状态的时候，若 store 中的状态发生变化，那么相应的组件也会相应地得到高效更新。</li>
<li>你不能直接改变 store 中的状态。改变 store 中的状态的唯一途径就是显式地提交 (commit) mutation。这样使得我们可以方便地跟踪每一个状态的变化，从而让我们能够实现一些工具帮助我们更好地了解我们的应用。<h2 id="通过npm-install-vuex-–save安装："><a href="#通过npm-install-vuex-–save安装：" class="headerlink" title="通过npm install vuex –save安装："></a>通过npm install vuex –save安装：</h2>先main.js文件中导入并注册store<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">import Vue from &#x27;vue&#x27;</span><br><span class="line">import App from &#x27;./App.vue&#x27;</span><br><span class="line">import store from &#x27;./store&#x27;</span><br><span class="line"></span><br><span class="line">Vue.config.productionTip = false</span><br><span class="line"></span><br><span class="line">new Vue(&#123;</span><br><span class="line">  // 为了在 Vue 组件中访问 this.$store property,你需要为 Vue 实例提供创建好的 store</span><br><span class="line">  store,</span><br><span class="line">  render: h =&gt; h(App)</span><br><span class="line">&#125;).$mount(&#x27;#app&#x27;)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
在创建一个.js文件实例化一个Store，并导出他<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">import Vue from &#x27;vue&#x27;</span><br><span class="line">import Vuex from &#x27;vuex&#x27;</span><br><span class="line"></span><br><span class="line">// 通过vue.use注册Vuex</span><br><span class="line">Vue.use(Vuex)</span><br><span class="line"></span><br><span class="line">// 实例化一个store对象</span><br><span class="line">export default new Vuex.Store(&#123;</span><br><span class="line">  // state类似于组件中的data，用来存放对象</span><br><span class="line">  // state中的对象只能通过mutations中的函数修改</span><br><span class="line">  state: &#123;</span><br><span class="line">    count:0</span><br><span class="line">  &#125;,</span><br><span class="line">  // getters类似于组件中的computed对象，用来处理数据</span><br><span class="line">  getters: &#123;</span><br><span class="line">  &#125;,</span><br><span class="line">  // mutations和actions类似于组件中的methods对象</span><br><span class="line">  // mutations中必须使用同步函数，mutations中的函数用于修改state中的对象</span><br><span class="line">  mutations: &#123;</span><br><span class="line">    increment (state) &#123;</span><br><span class="line">      state.count++</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  //  actions中的函数可以返回一个Promise，这也说明actions中可以使用异步函数的。</span><br><span class="line">  // actions中的函数不能直接修改state，需要调用mutations中的函数来修改。</span><br><span class="line">  // 通过commit调用mutations中的函数</span><br><span class="line">  actions: &#123;</span><br><span class="line">  &#125;,</span><br><span class="line">  // 模块对象。每个模块相当于一个Store的实例化对象</span><br><span class="line">  modules: &#123;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="获取数据，修改数据"><a href="#获取数据，修改数据" class="headerlink" title="获取数据，修改数据"></a>获取数据，修改数据</h2>获取数据<br>在组建方法中：this.$store.state.xxx<br>在标签中：$store.state.xxx或this.$store.state.xxx<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br><span class="line">318</span><br><span class="line">319</span><br><span class="line">320</span><br><span class="line">321</span><br><span class="line">322</span><br><span class="line">323</span><br><span class="line">324</span><br><span class="line">325</span><br><span class="line">326</span><br><span class="line">327</span><br><span class="line">328</span><br><span class="line">329</span><br><span class="line">330</span><br><span class="line">331</span><br><span class="line">332</span><br><span class="line">333</span><br><span class="line">334</span><br><span class="line">335</span><br><span class="line">336</span><br><span class="line">337</span><br><span class="line">338</span><br><span class="line">339</span><br><span class="line">340</span><br><span class="line">341</span><br><span class="line">342</span><br><span class="line">343</span><br><span class="line">344</span><br><span class="line">345</span><br><span class="line">346</span><br><span class="line">347</span><br><span class="line">348</span><br><span class="line">349</span><br><span class="line">350</span><br><span class="line">351</span><br><span class="line">352</span><br><span class="line">353</span><br><span class="line">354</span><br><span class="line">355</span><br><span class="line">356</span><br><span class="line">357</span><br><span class="line">358</span><br></pre></td><td class="code"><pre><span class="line">vuex介绍</span><br><span class="line">● state，驱动应用的数据源；相当于组件中的data</span><br><span class="line">● view，以声明方式将 state 映射到视图；相当于组件中的template</span><br><span class="line">● actions，响应在 view 上的用户输入导致的状态变化。相当于组件中的methods</span><br><span class="line"></span><br><span class="line">安装</span><br><span class="line">vue add vuex</span><br><span class="line">通过这种方式安装会自动配置相关信息</span><br><span class="line">npm install vuex --save</span><br><span class="line">通过这种方式安装，要自己配置vuex的相关信息</span><br><span class="line">使用</span><br><span class="line">1. Vuex 的状态存储是响应式的。当 Vue 组件从 store 中读取状态的时候，若 store 中的状态发生变化，那么相应的组件也会相应地得到高效更新。</span><br><span class="line">2. 你不能直接改变 store 中的状态。改变 store 中的状态的唯一途径就是显式地提交 (commit) mutation。这样使得我们可以方便地跟踪每一个状态的变化，从而让我们能够实现一些工具帮助我们更好地了解我们的应用。</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">通过npm install vuex --save安装：</span><br><span class="line">先main.js文件中导入并注册store</span><br><span class="line">import Vue from &#x27;vue&#x27;</span><br><span class="line">import App from &#x27;./App.vue&#x27;</span><br><span class="line">import store from &#x27;./store&#x27;</span><br><span class="line"></span><br><span class="line">Vue.config.productionTip = false</span><br><span class="line"></span><br><span class="line">new Vue(&#123;</span><br><span class="line">  // 为了在 Vue 组件中访问 this.$store property,你需要为 Vue 实例提供创建好的 store</span><br><span class="line">  store,</span><br><span class="line">  render: h =&gt; h(App)</span><br><span class="line">&#125;).$mount(&#x27;#app&#x27;)</span><br><span class="line"></span><br><span class="line">在创建一个.js文件实例化一个Store，并导出他</span><br><span class="line">import Vue from &#x27;vue&#x27;</span><br><span class="line">import Vuex from &#x27;vuex&#x27;</span><br><span class="line"></span><br><span class="line">// 通过vue.use注册Vuex</span><br><span class="line">Vue.use(Vuex)</span><br><span class="line"></span><br><span class="line">// 实例化一个store对象</span><br><span class="line">export default new Vuex.Store(&#123;</span><br><span class="line">  // state类似于组件中的data，用来存放对象</span><br><span class="line">  // state中的对象只能通过mutations中的函数修改</span><br><span class="line">  state: &#123;</span><br><span class="line">    count:0</span><br><span class="line">  &#125;,</span><br><span class="line">  // getters类似于组件中的computed对象，用来处理数据</span><br><span class="line">  getters: &#123;</span><br><span class="line">  &#125;,</span><br><span class="line">  // mutations和actions类似于组件中的methods对象</span><br><span class="line">  // mutations中必须使用同步函数，mutations中的函数用于修改state中的对象</span><br><span class="line">  mutations: &#123;</span><br><span class="line">    increment (state) &#123;</span><br><span class="line">      state.count++</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  //  actions中的函数可以返回一个Promise，这也说明actions中可以使用异步函数的。</span><br><span class="line">  // actions中的函数不能直接修改state，需要调用mutations中的函数来修改。</span><br><span class="line">  // 通过commit调用mutations中的函数</span><br><span class="line">  actions: &#123;</span><br><span class="line">  &#125;,</span><br><span class="line">  // 模块对象。每个模块相当于一个Store的实例化对象</span><br><span class="line">  modules: &#123;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">获取数据，修改数据</span><br><span class="line">获取数据</span><br><span class="line">在组建方法中：this.$store.state.xxx</span><br><span class="line">在标签中：$store.state.xxx或this.$store.state.xxx</span><br><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;div&gt;</span><br><span class="line">    //获取数据时可以加this，也可以不加，通过$store.state.xxx获取store中的数据</span><br><span class="line">    &lt;div&gt;&#123;&#123;this.$store.state.count&#125;&#125;&lt;/div&gt;</span><br><span class="line">    &lt;div&gt;&#123;&#123;$store.state.count&#125;&#125;&lt;/div&gt;</span><br><span class="line">  &lt;/div&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line">export default &#123;</span><br><span class="line">  mounted()&#123;</span><br><span class="line">    // 通过this.$store.commit的形式调用mutations中的increment函数修改state中的count对象</span><br><span class="line">    this.$store.commit(&#x27;increment&#x27;)</span><br><span class="line">    // 打印修改后的数据</span><br><span class="line">    console.log(this.$store.state.count)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;/script&gt;</span><br><span class="line">State</span><br><span class="line">Vuex 使用单一状态树——是的，用一个对象就包含了全部的应用层级状态。至此它便作为一个“唯一数据源 (SSOT(opens new window))”而存在。这也意味着，每个应用将仅仅包含一个 store 实例。单一状态树让我们能够直接地定位任一特定的状态片段，在调试的过程中也能轻易地取得整个当前应用状态的快照。</span><br><span class="line">存储在 Vuex 中的数据和 Vue 实例中的 data 遵循相同的规则</span><br><span class="line">在组件中获取vuex的state</span><br><span class="line">export default &#123;</span><br><span class="line">  mounted()&#123;</span><br><span class="line">    // 通过this.$store.commit的形式调用mutations中的increment函数修改state中的count对象</span><br><span class="line">    this.$store.commit(&#x27;increment&#x27;)</span><br><span class="line">    // 打印修改后的数据</span><br><span class="line">    console.log(this.$store.state.count)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">mapState辅助函数</span><br><span class="line">通过辅助函数一次获取多个state中的对象</span><br><span class="line">传入一个对象作为mapState参数</span><br><span class="line">// mapState时state的辅助函数</span><br><span class="line">  // 传入一个对象</span><br><span class="line">  computed:mapState(&#123;</span><br><span class="line">    // 这里可以使用箭头函数，通过函数将state中的对象返回给count</span><br><span class="line">    // 这样就可以在组件中通过count来显示state中的count对象的内容了</span><br><span class="line">    // 因为时响应式的所以数据会实时更新</span><br><span class="line">    count: function(state) &#123;return state.count&#125;,</span><br><span class="line"></span><br><span class="line">    // 通过别名方式获取state中的对象</span><br><span class="line">    // 这里的&#x27;count&#x27; 等同于 `state =&gt; state.count`</span><br><span class="line">    Acount:&#x27;count&#x27;,</span><br><span class="line"></span><br><span class="line">    // 为了能够使用 `this` 获取局部状态，必须使用常规函数</span><br><span class="line">    getLocalData(state)&#123;</span><br><span class="line">      return state.count + this.a</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;),</span><br><span class="line">传入一个数组作为mapState参数</span><br><span class="line">// 当映射的计算属性的名称与 state 的子节点名称相同时，可以给 mapState 传一个字符串数组</span><br><span class="line">  computed:mapState([</span><br><span class="line">    // 映射 this.count 为 store.state.count</span><br><span class="line">    &#x27;count&#x27;</span><br><span class="line">  ]),</span><br><span class="line">对象展开符</span><br><span class="line">computed:&#123;</span><br><span class="line">    // 通过对象展开符将mapState返回的对象展开</span><br><span class="line">    ...mapState(&#123;</span><br><span class="line">        count: function(state) &#123;return state.count&#125;,</span><br><span class="line">        Acount:&#x27;count&#x27;,</span><br><span class="line">        getLocalData(state)&#123;</span><br><span class="line">          return state.count + this.a</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;,</span><br><span class="line">getters</span><br><span class="line">// 处理数据，和组件中的computed类似</span><br><span class="line">// 通过getters将数据处理并保存，在组件中通过this.$store.getters.xxx获取对应的处理结果</span><br><span class="line">// this.$store.getters.newName --&gt; undefined123123(undefined是因为没有name)</span><br><span class="line">getters:&#123;</span><br><span class="line">  //第二个参数是一个getters</span><br><span class="line">  newName(state,get)&#123;</span><br><span class="line">    // 可以调用另一个getters</span><br><span class="line">    // get 代表getters</span><br><span class="line">    return state.name+ &quot;123&quot; + get.newName2;</span><br><span class="line">  &#125;,</span><br><span class="line">  newName2(state) &#123;</span><br><span class="line">    return &quot;123&quot;;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">获取getters中的数据</span><br><span class="line">mounted()&#123;</span><br><span class="line">    console.log(this.$store.getters.newName)</span><br><span class="line">  &#125;</span><br><span class="line">通过方法访问</span><br><span class="line">你也可以通过让 getter 返回一个函数，来实现给 getter 传参。在你对 store 里的数组进行查询时非常有用。</span><br><span class="line">getters: &#123;</span><br><span class="line">  getTodoById: function(state) &#123;</span><br><span class="line">    return (id) =&gt;&#123;</span><br><span class="line">      // todos是一个数组</span><br><span class="line">      return 	state.todos.find(todo =&gt; todo.id === id)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">使用</span><br><span class="line">mounted()&#123;</span><br><span class="line">    this.$store.getters.getTodoById(2)</span><br><span class="line">&#125;</span><br><span class="line">mapGetters辅助函数</span><br><span class="line">使用方法和mapState类似</span><br><span class="line">computed:&#123;</span><br><span class="line">  // 起别名</span><br><span class="line">  //   ...mapGetters(&#123;</span><br><span class="line">  //   someohter:&#x27;newName&#x27;,</span><br><span class="line">  // &#125;)</span><br><span class="line">  </span><br><span class="line">  //直接映射</span><br><span class="line">  ...mapGetters([</span><br><span class="line">    &#x27;newName&#x27;</span><br><span class="line">  ])</span><br><span class="line">&#125;,</span><br><span class="line">mutations</span><br><span class="line">Mutation 必须是同步函数</span><br><span class="line">接收参数</span><br><span class="line">mutations: &#123;</span><br><span class="line">    // 在大多数情况下，载荷应该是一个对象，</span><br><span class="line">    increment (state,payload) &#123;</span><br><span class="line">      state.count++;</span><br><span class="line">      console.log(payload);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">传入参数</span><br><span class="line">参数2可以是一个对像，也可以是多个参数</span><br><span class="line">mounted()&#123;</span><br><span class="line">    this.$store.commit(&#x27;increment&#x27;,&#123;&quot;a&quot;:12,&quot;b&quot;:21&#125;)</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">对象风格的提交方式</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">mounted()&#123;</span><br><span class="line">    this.$store.commit(&#123;</span><br><span class="line">      // type属性对应mutations中的方法</span><br><span class="line">      type:&#x27;increment&#x27;,</span><br><span class="line">      //剩下的属性都是参数，全部会被放到payload对象中</span><br><span class="line">      amount: 10,</span><br><span class="line">      account:&quot;hao123&quot;</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br><span class="line">Mutation 需遵守 Vue 的响应规则</span><br><span class="line">既然 Vuex 的 store 中的状态是响应式的，那么当我们变更状态时，监视状态的 Vue 组件也会自动更新。这也意味着 Vuex 中的 mutation 也需要与使用 Vue 一样遵守一些注意事项：</span><br><span class="line">1. 最好提前在你的 store 中初始化好所有所需属性。</span><br><span class="line">2. 当需要在对象上添加新属性时，你应该</span><br><span class="line">● 使用 Vue.set(obj, &#x27;newProp&#x27;, 123), 或者</span><br><span class="line">● 以新对象替换老对象。例如，利用对象展开运算符(opens new window)我们可以这样写：</span><br><span class="line">state.obj = &#123; ...state.obj, newProp: 123 &#125;</span><br><span class="line">使用常量替代 Mutation 事件类型</span><br><span class="line">export const SOME_MUTATION = &#x27;SOME_MUTATION&#x27;;</span><br><span class="line">export const mutations= &#123;</span><br><span class="line">  // 在大多数情况下，载荷应该是一个对象，</span><br><span class="line">  increment (state,payload) &#123;</span><br><span class="line">    state.count++;</span><br><span class="line">    console.log(payload);</span><br><span class="line">    console.log(state)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">在store的js文件中导入</span><br><span class="line"></span><br><span class="line">在组件中提交 Mutation</span><br><span class="line">mapMutations 辅助函数</span><br><span class="line">methods:&#123;</span><br><span class="line">    ...mapMutations([</span><br><span class="line">      &#x27;increment&#x27;// 将 `this.increment()` 映射为 `this.$store.commit(&#x27;increment&#x27;)`</span><br><span class="line">    ]),</span><br><span class="line">    ...mapMutations(&#123;</span><br><span class="line">      // 别名</span><br><span class="line">      add: &#x27;increment&#x27; // 将 `this.add()` 映射为 `this.$store.commit(&#x27;increment&#x27;)`</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;</span><br><span class="line">使用</span><br><span class="line">mounted()&#123;</span><br><span class="line">    this.increment();</span><br><span class="line">    this.add()</span><br><span class="line">&#125;</span><br><span class="line">action</span><br><span class="line">可以在 action 内部执行异步操作</span><br><span class="line"></span><br><span class="line">使用</span><br><span class="line">Action 函数接受一个与 store 实例具有相同方法和属性的 context 对象，因此你可以调用 context.commit 提交一个 mutation，或者通过 context.state 和 context.getters 来获取 state 和 getters。</span><br><span class="line">actions: &#123;</span><br><span class="line">    // 这里的context相当于是store </span><br><span class="line">    increment (context) &#123;</span><br><span class="line">      //commit中的increment是mutations中的函数</span><br><span class="line">      context.commit(&#x27;increment&#x27;)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">实践中，我们会经常用到 ES2015 的 参数解构(opens new window)来简化代码（特别是我们需要调用 commit 很多次的时候）：</span><br><span class="line">actions: &#123;</span><br><span class="line">  // 通过解构获取到commit对象--》const &#123;commit&#125; = store</span><br><span class="line">  increment (&#123; commit &#125;) &#123;</span><br><span class="line">    commit(&#x27;increment&#x27;)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">触发action</span><br><span class="line">mounted()&#123;</span><br><span class="line">    // 触发action中的方法</span><br><span class="line">    this.$store.dispatch(&#x27;increment&#x27;)</span><br><span class="line">  &#125;</span><br><span class="line">Actions 支持同样的载荷方式和对象方式进行分发</span><br><span class="line"></span><br><span class="line">mounted()&#123;</span><br><span class="line">  // 以载荷形式分发</span><br><span class="line">    this.$store.dispatch(&#x27;increment&#x27;,&#123;&quot;a&quot;:123,&quot;b&quot;:456&#125;)</span><br><span class="line">  </span><br><span class="line">  // 以对象形式分发</span><br><span class="line">  this.$store.dispatch(&#123;</span><br><span class="line">    type: &#x27;increment&#x27;,</span><br><span class="line">    &quot;a&quot;:123,</span><br><span class="line">    &quot;b&quot;:456</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">接收方式也是一样的</span><br><span class="line">actions: &#123;</span><br><span class="line">    increment (context,payload) &#123;</span><br><span class="line">      context.commit(&#x27;increment&#x27;,payload)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line"></span><br><span class="line">在组件中分发 Action</span><br><span class="line">mapActions 辅助函数</span><br><span class="line">methods:&#123;</span><br><span class="line">    ...mapActions ([</span><br><span class="line">      &#x27;increment&#x27;,</span><br><span class="line">    ]),</span><br><span class="line">    ...mapActions (&#123;</span><br><span class="line">      add:&#x27;increment&#x27;,</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;,</span><br><span class="line">store.dispatch</span><br><span class="line">store.dispatch返回一个Promise</span><br><span class="line">action中的函数也可以返回一个Promise</span><br><span class="line">actions: &#123;</span><br><span class="line">  actionA (&#123; commit &#125;) &#123;</span><br><span class="line">    return new Promise((resolve, reject) =&gt; &#123;</span><br><span class="line">      setTimeout(() =&gt; &#123;</span><br><span class="line">        commit(&#x27;someMutation&#x27;)</span><br><span class="line">        resolve()</span><br><span class="line">      &#125;, 1000)</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">store.dispatch(&#x27;actionA&#x27;)返回一个Promise</span><br><span class="line">actions中的函数亦可以使用dispatch触发另一个action</span><br><span class="line">actions: &#123;</span><br><span class="line">  // ...</span><br><span class="line">  actionB (&#123; dispatch, commit &#125;) &#123;</span><br><span class="line">    return dispatch(&#x27;actionA&#x27;).then(() =&gt; &#123;</span><br><span class="line">      commit(&#x27;someOtherMutation&#x27;)</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">使用async await处理异步函数</span><br><span class="line">actions: &#123;</span><br><span class="line">  async actionA (&#123; commit &#125;) &#123;</span><br><span class="line">    commit(&#x27;gotData&#x27;, await getData())</span><br><span class="line">  &#125;,</span><br><span class="line">  async actionB (&#123; dispatch, commit &#125;) &#123;</span><br><span class="line">    await dispatch(&#x27;actionA&#x27;) // 等待 actionA 完成</span><br><span class="line">    commit(&#x27;gotOtherData&#x27;, await getOtherData())</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">module</span><br><span class="line">由于使用单一状态树，应用的所有状态会集中到一个比较大的对象。当应用变得非常复杂时，store 对象就有可能变得相当臃肿。</span><br><span class="line">为了解决以上问题，Vuex 允许我们将 store 分割成模块（module）。每个模块拥有自己的 state、mutation、action、getter、甚至是嵌套子模块——从上至下进行同样方式的分割：</span><br><span class="line">const moduleA = &#123;</span><br><span class="line">  state: () =&gt; (&#123; ... &#125;),</span><br><span class="line">  mutations: &#123; ... &#125;,</span><br><span class="line">  actions: &#123; ... &#125;,</span><br><span class="line">  getters: &#123; ... &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const moduleB = &#123;</span><br><span class="line">  state: () =&gt; (&#123; ... &#125;),</span><br><span class="line">  mutations: &#123; ... &#125;,</span><br><span class="line">  actions: &#123; ... &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const store = new Vuex.Store(&#123;</span><br><span class="line">  modules: &#123;</span><br><span class="line">    a: moduleA,</span><br><span class="line">    b: moduleB</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">store.state.a // -&gt; moduleA 的状态</span><br><span class="line">store.state.b // -&gt; moduleB 的状态</span><br></pre></td></tr></table></figure>
<h1 id="State"><a href="#State" class="headerlink" title="State"></a>State</h1>Vuex 使用单一状态树——是的，用一个对象就包含了全部的应用层级状态。至此它便作为一个“唯一数据源 (SSOT(opens new window))”而存在。这也意味着，每个应用将仅仅包含一个 store 实例。单一状态树让我们能够直接地定位任一特定的状态片段，在调试的过程中也能轻易地取得整个当前应用状态的快照。<br>存储在 Vuex 中的数据和 Vue 实例中的 data 遵循相同的规则<h2 id="在组件中获取vuex的state"><a href="#在组件中获取vuex的state" class="headerlink" title="在组件中获取vuex的state"></a>在组件中获取vuex的state</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">export default &#123;</span><br><span class="line">  mounted()&#123;</span><br><span class="line">    // 通过this.$store.commit的形式调用mutations中的increment函数修改state中的count对象</span><br><span class="line">    this.$store.commit(&#x27;increment&#x27;)</span><br><span class="line">    // 打印修改后的数据</span><br><span class="line">    console.log(this.$store.state.count)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="mapState辅助函数"><a href="#mapState辅助函数" class="headerlink" title="mapState辅助函数"></a>mapState辅助函数</h2>通过辅助函数一次获取多个state中的对象<h3 id="传入一个对象作为mapState参数"><a href="#传入一个对象作为mapState参数" class="headerlink" title="传入一个对象作为mapState参数"></a>传入一个对象作为mapState参数</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">// mapState时state的辅助函数</span><br><span class="line">  // 传入一个对象</span><br><span class="line">  computed:mapState(&#123;</span><br><span class="line">    // 这里可以使用箭头函数，通过函数将state中的对象返回给count</span><br><span class="line">    // 这样就可以在组件中通过count来显示state中的count对象的内容了</span><br><span class="line">    // 因为时响应式的所以数据会实时更新</span><br><span class="line">    count: function(state) &#123;return state.count&#125;,</span><br><span class="line"></span><br><span class="line">    // 通过别名方式获取state中的对象</span><br><span class="line">    // 这里的&#x27;count&#x27; 等同于 `state =&gt; state.count`</span><br><span class="line">    Acount:&#x27;count&#x27;,</span><br><span class="line"></span><br><span class="line">    // 为了能够使用 `this` 获取局部状态，必须使用常规函数</span><br><span class="line">    getLocalData(state)&#123;</span><br><span class="line">      return state.count + this.a</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;),</span><br></pre></td></tr></table></figure>
<h3 id="传入一个数组作为mapState参数"><a href="#传入一个数组作为mapState参数" class="headerlink" title="传入一个数组作为mapState参数"></a>传入一个数组作为mapState参数</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">// 当映射的计算属性的名称与 state 的子节点名称相同时，可以给 mapState 传一个字符串数组</span><br><span class="line">  computed:mapState([</span><br><span class="line">    // 映射 this.count 为 store.state.count</span><br><span class="line">    &#x27;count&#x27;</span><br><span class="line">  ]),</span><br></pre></td></tr></table></figure>
<h3 id="对象展开符"><a href="#对象展开符" class="headerlink" title="对象展开符"></a>对象展开符</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">computed:&#123;</span><br><span class="line">    // 通过对象展开符将mapState返回的对象展开</span><br><span class="line">    ...mapState(&#123;</span><br><span class="line">        count: state =&gt; state.count,</span><br><span class="line">        Acount:&#x27;count&#x27;,</span><br><span class="line">        getLocalData(state)&#123;</span><br><span class="line">          return state.count + this.a</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;,</span><br></pre></td></tr></table></figure>
<h1 id="getters"><a href="#getters" class="headerlink" title="getters"></a>getters</h1><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">// 处理数据，和组件中的computed类似</span><br><span class="line">// 通过getters将数据处理并保存，在组件中通过this.$store.getters.xxx获取对应的处理结果</span><br><span class="line">// this.$store.getters.newName --&gt; undefined123123(undefined是因为没有name)</span><br><span class="line">getters:&#123;</span><br><span class="line">  //第二个参数是一个getters</span><br><span class="line">  newName(state,get)&#123;</span><br><span class="line">    // 可以调用另一个getters</span><br><span class="line">    // get 代表getters</span><br><span class="line">    return state.name+ &quot;123&quot; + get.newName2;</span><br><span class="line">  &#125;,</span><br><span class="line">  newName2(state) &#123;</span><br><span class="line">    return &quot;123&quot;;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="获取getters中的数据"><a href="#获取getters中的数据" class="headerlink" title="获取getters中的数据"></a>获取getters中的数据</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">mounted()&#123;</span><br><span class="line">    console.log(this.$store.getters.newName)</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>
<h2 id="通过方法访问"><a href="#通过方法访问" class="headerlink" title="通过方法访问"></a>通过方法访问</h2>你也可以通过让 getter 返回一个函数，来实现给 getter 传参。在你对 store 里的数组进行查询时非常有用。<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">getters: &#123;</span><br><span class="line">  getTodoById: function(state) &#123;</span><br><span class="line">    return (id) =&gt;&#123;</span><br><span class="line">      // todos是一个数组</span><br><span class="line">      return 	state.todos.find(todo =&gt; todo.id === id)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="使用-1"><a href="#使用-1" class="headerlink" title="使用"></a>使用</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">mounted()&#123;</span><br><span class="line">    this.$store.getters.getTodoById(2)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="mapGetters辅助函数"><a href="#mapGetters辅助函数" class="headerlink" title="mapGetters辅助函数"></a>mapGetters辅助函数</h2>使用方法和mapState类似<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">computed:&#123;</span><br><span class="line">  // 起别名</span><br><span class="line">  //   ...mapGetters(&#123;</span><br><span class="line">  //   someohter:&#x27;newName&#x27;,</span><br><span class="line">  // &#125;)</span><br><span class="line">  </span><br><span class="line">  //直接映射</span><br><span class="line">  ...mapGetters([</span><br><span class="line">    &#x27;newName&#x27;</span><br><span class="line">  ])</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>
<h1 id="mutations"><a href="#mutations" class="headerlink" title="mutations"></a>mutations</h1><h2 id="Mutation-必须是同步函数"><a href="#Mutation-必须是同步函数" class="headerlink" title="Mutation 必须是同步函数"></a>Mutation 必须是同步函数</h2><h2 id="接收参数"><a href="#接收参数" class="headerlink" title="接收参数"></a>接收参数</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">mutations: &#123;</span><br><span class="line">    // 在大多数情况下，载荷应该是一个对象，</span><br><span class="line">    increment (state,payload) &#123;</span><br><span class="line">      state.count++;</span><br><span class="line">      console.log(payload);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br></pre></td></tr></table></figure>
<h2 id="传入参数"><a href="#传入参数" class="headerlink" title="传入参数"></a>传入参数</h2>参数2可以是一个对像，也可以是多个参数<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">mounted()&#123;</span><br><span class="line">    this.$store.commit(&#x27;increment&#x27;,&#123;&quot;a&quot;:12,&quot;b&quot;:21&#125;)</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>
<h2 id="对象风格的提交方式"><a href="#对象风格的提交方式" class="headerlink" title="对象风格的提交方式"></a>对象风格的提交方式</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">mounted()&#123;</span><br><span class="line">    this.$store.commit(&#123;</span><br><span class="line">      // type属性对应mutations中的方法</span><br><span class="line">      type:&#x27;increment&#x27;,</span><br><span class="line">      //剩下的属性都是参数，全部会被放到payload对象中</span><br><span class="line">      amount: 10,</span><br><span class="line">      account:&quot;hao123&quot;</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="Mutation-需遵守-Vue-的响应规则"><a href="#Mutation-需遵守-Vue-的响应规则" class="headerlink" title="Mutation 需遵守 Vue 的响应规则"></a>Mutation 需遵守 Vue 的响应规则</h2>既然 Vuex 的 store 中的状态是响应式的，那么当我们变更状态时，监视状态的 Vue 组件也会自动更新。这也意味着 Vuex 中的 mutation 也需要与使用 Vue 一样遵守一些注意事项：</li>
<li>最好提前在你的 store 中初始化好所有所需属性。</li>
<li>当需要在对象上添加新属性时，你应该<br>● 使用 Vue.set(obj, ‘newProp’, 123), 或者<br>● 以新对象替换老对象。例如，利用对象展开运算符(opens new window)我们可以这样写：<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">state.obj = &#123; ...state.obj, newProp: 123 &#125;</span><br></pre></td></tr></table></figure>
<h2 id="使用常量替代-Mutation-事件类型"><a href="#使用常量替代-Mutation-事件类型" class="headerlink" title="使用常量替代 Mutation 事件类型"></a>使用常量替代 Mutation 事件类型</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">export const SOME_MUTATION = &#x27;SOME_MUTATION&#x27;;</span><br><span class="line">export const mutations= &#123;</span><br><span class="line">  // 在大多数情况下，载荷应该是一个对象，</span><br><span class="line">  increment (state,payload) &#123;</span><br><span class="line">    state.count++;</span><br><span class="line">    console.log(payload);</span><br><span class="line">    console.log(state)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
在store的js文件中导入</li>
</ol>
<h2 id="在组件中提交-Mutation"><a href="#在组件中提交-Mutation" class="headerlink" title="在组件中提交 Mutation"></a>在组件中提交 Mutation</h2><h3 id="mapMutations-辅助函数"><a href="#mapMutations-辅助函数" class="headerlink" title="mapMutations 辅助函数"></a>mapMutations 辅助函数</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">methods:&#123;</span><br><span class="line">    ...mapMutations([</span><br><span class="line">      &#x27;increment&#x27;// 将 `this.increment()` 映射为 `this.$store.commit(&#x27;increment&#x27;)`</span><br><span class="line">    ]),</span><br><span class="line">    ...mapMutations(&#123;</span><br><span class="line">      // 别名</span><br><span class="line">      add: &#x27;increment&#x27; // 将 `this.add()` 映射为 `this.$store.commit(&#x27;increment&#x27;)`</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>
<p>使用</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">mounted()&#123;</span><br><span class="line">    this.increment();</span><br><span class="line">    this.add()</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="action"><a href="#action" class="headerlink" title="action"></a>action</h1><h2 id="可以在-action-内部执行异步操作"><a href="#可以在-action-内部执行异步操作" class="headerlink" title="可以在 action 内部执行异步操作"></a>可以在 action 内部执行异步操作</h2><h2 id="使用-2"><a href="#使用-2" class="headerlink" title="使用"></a>使用</h2><p>Action 函数接受一个与 store 实例具有相同方法和属性的 context 对象，因此你可以调用 context.commit 提交一个 mutation，或者通过 context.state 和 context.getters 来获取 state 和 getters。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">actions: &#123;</span><br><span class="line">    // 这里的context相当于是store </span><br><span class="line">    increment (context) &#123;</span><br><span class="line">      //commit中的increment是mutations中的函数</span><br><span class="line">      context.commit(&#x27;increment&#x27;)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br></pre></td></tr></table></figure>
<p>实践中，我们会经常用到 ES2015 的 参数解构(opens new window)来简化代码（特别是我们需要调用 commit 很多次的时候）：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">actions: &#123;</span><br><span class="line">  // 通过解构获取到commit对象--》const &#123;commit&#125; = store</span><br><span class="line">  increment (&#123; commit &#125;) &#123;</span><br><span class="line">    commit(&#x27;increment&#x27;)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="触发action"><a href="#触发action" class="headerlink" title="触发action"></a>触发action</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">mounted()&#123;</span><br><span class="line">    // 触发action中的方法</span><br><span class="line">    this.$store.dispatch(&#x27;increment&#x27;)</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>
<h2 id="Actions-支持同样的载荷方式和对象方式进行分发"><a href="#Actions-支持同样的载荷方式和对象方式进行分发" class="headerlink" title="Actions 支持同样的载荷方式和对象方式进行分发"></a>Actions 支持同样的载荷方式和对象方式进行分发</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">mounted()&#123;</span><br><span class="line">  // 以载荷形式分发</span><br><span class="line">    this.$store.dispatch(&#x27;increment&#x27;,&#123;&quot;a&quot;:123,&quot;b&quot;:456&#125;)</span><br><span class="line">  </span><br><span class="line">  // 以对象形式分发</span><br><span class="line">  this.$store.dispatch(&#123;</span><br><span class="line">    type: &#x27;increment&#x27;,</span><br><span class="line">    &quot;a&quot;:123,</span><br><span class="line">    &quot;b&quot;:456</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>接收方式也是一样的</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">actions: &#123;</span><br><span class="line">    increment (context,payload) &#123;</span><br><span class="line">      context.commit(&#x27;increment&#x27;,payload)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br></pre></td></tr></table></figure>
<h2 id="在组件中分发-Action"><a href="#在组件中分发-Action" class="headerlink" title="在组件中分发 Action"></a>在组件中分发 Action</h2><h3 id="mapActions-辅助函数"><a href="#mapActions-辅助函数" class="headerlink" title="mapActions 辅助函数"></a>mapActions 辅助函数</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">methods:&#123;</span><br><span class="line">    ...mapActions ([</span><br><span class="line">      &#x27;increment&#x27;,</span><br><span class="line">    ]),</span><br><span class="line">    ...mapActions (&#123;</span><br><span class="line">      add:&#x27;increment&#x27;,</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;,</span><br></pre></td></tr></table></figure>
<h2 id="store-dispatch"><a href="#store-dispatch" class="headerlink" title="store.dispatch"></a>store.dispatch</h2><p>store.dispatch返回一个Promise<br>action中的函数也可以返回一个Promise</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">actions: &#123;</span><br><span class="line">  actionA (&#123; commit &#125;) &#123;</span><br><span class="line">    return new Promise((resolve, reject) =&gt; &#123;</span><br><span class="line">      setTimeout(() =&gt; &#123;</span><br><span class="line">        commit(&#x27;someMutation&#x27;)</span><br><span class="line">        resolve()</span><br><span class="line">      &#125;, 1000)</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">store.dispatch(&#x27;actionA&#x27;)返回一个Promise</span><br></pre></td></tr></table></figure>
<p>actions中的函数亦可以使用dispatch触发另一个action</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">actions: &#123;</span><br><span class="line">  // ...</span><br><span class="line">  actionB (&#123; dispatch, commit &#125;) &#123;</span><br><span class="line">    return dispatch(&#x27;actionA&#x27;).then(() =&gt; &#123;</span><br><span class="line">      commit(&#x27;someOtherMutation&#x27;)</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="使用async-await处理异步函数"><a href="#使用async-await处理异步函数" class="headerlink" title="使用async await处理异步函数"></a>使用async await处理异步函数</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">actions: &#123;</span><br><span class="line">  async actionA (&#123; commit &#125;) &#123;</span><br><span class="line">    commit(&#x27;gotData&#x27;, await getData())</span><br><span class="line">  &#125;,</span><br><span class="line">  async actionB (&#123; dispatch, commit &#125;) &#123;</span><br><span class="line">    await dispatch(&#x27;actionA&#x27;) // 等待 actionA 完成</span><br><span class="line">    commit(&#x27;gotOtherData&#x27;, await getOtherData())</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2022/03/23/vuex%E5%AD%A6%E4%B9%A0/" data-id="cl13n59r00000ygw620qe59wc" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
  
    <a href="/2022/03/22/vuew-router%E7%9A%84%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">vuew-router的基本使用</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/03/">March 2022</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2022/03/23/vuex%E5%AD%A6%E4%B9%A0/">vuex学习</a>
          </li>
        
          <li>
            <a href="/2022/03/22/vuew-router%E7%9A%84%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8/">vuew-router的基本使用</a>
          </li>
        
          <li>
            <a href="/2022/03/22/vue-router%E7%9A%84%E5%88%9B%E5%BB%BA%E8%BF%87%E7%A8%8B/">vue-router的创建过程</a>
          </li>
        
          <li>
            <a href="/2022/03/20/axios%E5%AD%A6%E4%B9%A0/">axios学习</a>
          </li>
        
          <li>
            <a href="/2022/03/16/%E4%BA%86%E8%A7%A3vue%E7%BB%84%E4%BB%B6/">了解vue组件</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2022 诶无痕和诶有痕<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="/fancybox/jquery.fancybox.css">

  
<script src="/fancybox/jquery.fancybox.pack.js"></script>




<script src="/js/script.js"></script>




  </div>
</body>
</html>